#coding=utf-8
from stockdb.misc import *

code = '600000'
n = 20

held = False
money = 10000.0
ds = 0
top0 = 0
r = 2.5

n2 = 0
s = find_stock(code)
tds = s.tds
tds.reverse()
print s.name, code, tds[0],len(tds), s.iprice

for td in tds[n:]:

    c = td.price.fq('close')
    pre_td = td.pre
    
    if not held:
        #大盘
        dt = td.date.isoformat()
        std = szzs.find_td(dt)
        if std.price.close > std.avg_price(20):
        #
            top = pre_td.top(n, item='highest', fq=True)
            if c > top:
                ds = money/c
                held = True
                top0 = top
                print '买进: %s. 点位:%.2f'%(td, c)        
    else:
        N = pre_td.get_N(fq=True)
        zs = top0 - r*N
        if c < zs:
            money = ds * c
            held = False
            top0 = 0
            print '卖出: %s. 点位:%.2f;money:%.1f\n'%(td, c, money) 
        else:
            h = td.price.fq('highest')
            if h > top0:
                top0 = h
                print '%s:新高:%.2f'%(td, top0)
        
        n2 = n2 + 1
print n2
